import { get, post, put, del } from '@/utils/request';

/**
 * 系统管理API服务
 */

// ==================== 通知管理 ====================

/**
 * 获取通知列表
 */
export async function getNotificationList(params: SYSTEM.NotificationListParams): Promise<SYSTEM.ApiResponse<{ list: SYSTEM.NotificationModel[]; pagination: SYSTEM.PaginationInfo }>> {
  return get('/admin/notifications', params);
}

/**
 * 获取通知详情
 */
export async function getNotificationDetail(id: number): Promise<SYSTEM.ApiResponse<SYSTEM.NotificationModel>> {
  return get(`/admin/notifications/${id}`);
}

/**
 * 创建通知
 */
export async function createNotification(data: SYSTEM.CreateNotificationParams): Promise<SYSTEM.ApiResponse<SYSTEM.NotificationModel>> {
  return post('/admin/notifications', data);
}

/**
 * 更新通知
 */
export async function updateNotification(id: number, data: SYSTEM.UpdateNotificationParams): Promise<SYSTEM.ApiResponse<SYSTEM.NotificationModel>> {
  return put(`/admin/notifications/${id}`, data);
}

/**
 * 删除通知
 */
export async function deleteNotification(id: number): Promise<SYSTEM.ApiResponse<null>> {
  return del(`/admin/notifications/${id}`);
}

/**
 * 发送通知
 */
export async function sendNotification(id: number): Promise<SYSTEM.ApiResponse<{ sent_count: number }>> {
  return post(`/admin/notifications/${id}/send`);
}

/**
 * 获取通知发送状态
 */
export async function getNotificationSendStatus(id: number): Promise<SYSTEM.ApiResponse<any>> {
  return get(`/admin/notifications/${id}/send-status`);
}

/**
 * 预览通知
 */
export async function previewNotification(data: SYSTEM.CreateNotificationParams): Promise<SYSTEM.ApiResponse<{ preview_html: string; recipient_count: number }>> {
  return post('/admin/notifications/preview', data);
}

// ==================== 文件管理 ====================

/**
 * 获取文件列表
 */
export async function getFileList(params: SYSTEM.FileListParams): Promise<SYSTEM.ApiResponse<{ list: SYSTEM.FileUploadModel[]; pagination: SYSTEM.PaginationInfo }>> {
  return get('/admin/files', params);
}

/**
 * 获取文件详情
 */
export async function getFileDetail(id: number): Promise<SYSTEM.ApiResponse<SYSTEM.FileUploadModel>> {
  return get(`/admin/files/${id}`);
}

/**
 * 上传文件
 */
export async function uploadFile(data: SYSTEM.FileUploadParams): Promise<SYSTEM.ApiResponse<SYSTEM.FileUploadModel>> {
  const formData = new FormData();
  formData.append('file', data.file);
  formData.append('usage_type', data.usage_type);
  if (data.description) {
    formData.append('description', data.description);
  }

  return post('/admin/files/upload', formData);
}

/**
 * 上传图片 - 专用于图片上传的接口
 */
export async function uploadImage(file: File): Promise<SYSTEM.ApiResponse<{ url: string }>> {
  const formData = new FormData();
  formData.append('file', file);

  return post('/upload/image', formData);
}

/**
 * 上传视频文件 - 专用于视频文件上传的接口
 */
export async function uploadVideo(file: File): Promise<SYSTEM.ApiResponse<{
  url: string;
  filename: string;
  size: number;
  duration?: number;
  format: string;
  thumbnail?: string;
  id: number;
}>> {
  const formData = new FormData();
  formData.append('file', file);

  return post('/upload/video', formData);
}

/**
 * 上传音频文件 - 专用于音频文件上传的接口
 */
export async function uploadAudio(file: File): Promise<SYSTEM.ApiResponse<{
  url: string;
  filename: string;
  size: number;
  duration?: number;
  format: string;
  id: number;
}>> {
  const formData = new FormData();
  formData.append('file', file);

  return post('/upload/audio', formData);
}

/**
 * 删除文件
 */
export async function deleteFile(id: number): Promise<SYSTEM.ApiResponse<null>> {
  return del(`/admin/files/${id}`);
}

/**
 * 批量删除文件
 */
export async function batchDeleteFiles(ids: number[]): Promise<SYSTEM.ApiResponse<null>> {
  return post('/admin/files/batch-delete', { file_ids: ids });
}

/**
 * 获取文件使用情况
 */
export async function getFileUsage(id: number): Promise<SYSTEM.ApiResponse<any>> {
  return get(`/admin/files/${id}/usage`);
}

/**
 * 清理未使用的文件
 */
export async function cleanupUnusedFiles(): Promise<SYSTEM.ApiResponse<{ deleted_count: number; freed_space: number }>> {
  return post('/admin/files/cleanup-unused');
}

// ==================== 成就管理 ====================

/**
 * 获取成就定义列表
 */
export async function getAchievementList(params: SYSTEM.AchievementListParams): Promise<SYSTEM.ApiResponse<{ list: SYSTEM.AchievementDefinitionModel[]; pagination: SYSTEM.PaginationInfo }>> {
  return get('/admin/achievements', params);
}

/**
 * 获取成就定义详情
 */
export async function getAchievementDetail(id: number): Promise<SYSTEM.ApiResponse<SYSTEM.AchievementDefinitionModel>> {
  return get(`/admin/achievements/${id}`);
}

/**
 * 创建成就定义
 */
export async function createAchievement(data: SYSTEM.CreateAchievementParams): Promise<SYSTEM.ApiResponse<SYSTEM.AchievementDefinitionModel>> {
  return post('/admin/achievements', data);
}

/**
 * 更新成就定义
 */
export async function updateAchievement(id: number, data: SYSTEM.UpdateAchievementParams): Promise<SYSTEM.ApiResponse<SYSTEM.AchievementDefinitionModel>> {
  return put(`/admin/achievements/${id}`, data);
}

/**
 * 删除成就定义
 */
export async function deleteAchievement(id: number): Promise<SYSTEM.ApiResponse<null>> {
  return del(`/admin/achievements/${id}`);
}

/**
 * 更新成就状态
 */
export async function updateAchievementStatus(id: number, status: 'active' | 'inactive'): Promise<SYSTEM.ApiResponse<null>> {
  return put(`/admin/achievements/${id}/status`, { status });
}

/**
 * 获取成就解锁记录
 */
export async function getAchievementUnlockRecords(id: number, params?: { page?: number; limit?: number }): Promise<SYSTEM.ApiResponse<any>> {
  return get(`/admin/achievements/${id}/unlock-records`, params);
}

/**
 * 手动授予成就
 */
export async function grantAchievement(achievementId: number, userIds: number[], reason?: string): Promise<SYSTEM.ApiResponse<{ granted_count: number }>> {
  return post(`/admin/achievements/${achievementId}/grant`, { user_ids: userIds, reason });
}

/**
 * 撤销成就
 */
export async function revokeAchievement(achievementId: number, userIds: number[], reason?: string): Promise<SYSTEM.ApiResponse<{ revoked_count: number }>> {
  return post(`/admin/achievements/${achievementId}/revoke`, { user_ids: userIds, reason });
}

// ==================== 系统设置 ====================

/**
 * 获取系统设置列表
 */
export async function getSystemSettingList(params: SYSTEM.SystemSettingListParams): Promise<SYSTEM.ApiResponse<SYSTEM.SystemSettingModel[]>> {
  return get('/admin/system-settings', params);
}

/**
 * 获取系统设置详情
 */
export async function getSystemSettingDetail(key: string): Promise<SYSTEM.ApiResponse<SYSTEM.SystemSettingModel>> {
  return get(`/admin/system-settings/${key}`);
}

/**
 * 更新系统设置
 */
export async function updateSystemSetting(key: string, data: SYSTEM.UpdateSystemSettingParams): Promise<SYSTEM.ApiResponse<SYSTEM.SystemSettingModel>> {
  return put(`/admin/system-settings/${key}`, data);
}

/**
 * 批量更新系统设置
 */
export async function batchUpdateSystemSettings(settings: Array<{ key: string; value: string }>): Promise<SYSTEM.ApiResponse<null>> {
  return put('/admin/system-settings/batch-update', { settings });
}

/**
 * 重置系统设置为默认值
 */
export async function resetSystemSetting(key: string): Promise<SYSTEM.ApiResponse<SYSTEM.SystemSettingModel>> {
  return post(`/admin/system-settings/${key}/reset`);
}

/**
 * 导出系统设置
 */
export async function exportSystemSettings(format: 'json' | 'yaml'): Promise<Blob> {
  return get('/admin/system-settings/export', { format }, { responseType: 'blob' });
}

/**
 * 导入系统设置
 */
export async function importSystemSettings(file: File): Promise<SYSTEM.ApiResponse<{ updated_count: number; errors: any[] }>> {
  const formData = new FormData();
  formData.append('file', file);

  return post('/admin/system-settings/import', formData);
}

// ==================== 系统统计 ====================

/**
 * 获取系统统计数据
 */
export async function getSystemStatistics(): Promise<SYSTEM.ApiResponse<SYSTEM.SystemStatistics>> {
  return get('/admin/statistics/system');
}

// ==================== 通用功能 ====================

/**
 * 获取通知类型选项
 */
export async function getNotificationTypeOptions(): Promise<SYSTEM.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/system/notification-type-options');
}

/**
 * 获取目标类型选项
 */
export async function getTargetTypeOptions(): Promise<SYSTEM.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/system/target-type-options');
}

/**
 * 获取文件使用类型选项
 */
export async function getFileUsageTypeOptions(): Promise<SYSTEM.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/system/file-usage-type-options');
}

/**
 * 获取成就类型选项
 */
export async function getAchievementTypeOptions(): Promise<SYSTEM.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/system/achievement-type-options');
}

/**
 * 获取奖励类型选项
 */
export async function getRewardTypeOptions(): Promise<SYSTEM.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/system/reward-type-options');
}
